// ignore: import_of_legacy_library_into_null_safe
// ignore_for_file: deprecated_member_use

// ignore: import_of_legacy_library_into_null_safe
import 'package:animations/animations.dart';
import 'package:flutter/material.dart';
import 'package:flutter_widget/custom/custom_appbar.dart';

// ignore: must_be_immutable
class GradientAnimation extends StatefulWidget {
  String name;
  GradientAnimation({Key? key, required this.name}) : super(key: key);

  @override
  _GradientAnimationState createState() => _GradientAnimationState();
}

class _GradientAnimationState extends State<GradientAnimation>
    with SingleTickerProviderStateMixin {
  late AnimationController controller;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
        value: 0,
        duration: const Duration(milliseconds: 2000),
        reverseDuration: const Duration(milliseconds: 2000),
        vsync: this)
      ..addStatusListener((status) {
        setState(() {});
      });
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  bool get isForwordAnimation =>
      controller.status == AnimationStatus.forward ||
      controller.status == AnimationStatus.completed;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppbar(widget.name, Theme.of(context).primaryColor),
      body: Center(
        child: Column(
          children: [
            const SizedBox(
              height: 20,
            ),
            ElevatedButton(
              onPressed: () => showCustomDialog(context),
              child: const Text(
                '渐变动画Dialog',
                style: TextStyle(color: Colors.white),
              ),
            ),
            const SizedBox(
              height: 20,
            ),
            ElevatedButton(
              onPressed: () => toggle(),
              child: Text(
                isForwordAnimation ? '影藏浮动按钮' : '显示浮动按钮',
                style: const TextStyle(color: Colors.white),
              ),
            ),
            const SizedBox(
              height: 20,
            ),
          ],
        ),
      ),
      floatingActionButton: buildFloatingActionButton(),
    );
  }

  Future showCustomDialog(BuildContext context) => showModal(
        configuration: const FadeScaleTransitionConfiguration(
          transitionDuration: Duration(seconds: 2),
          reverseTransitionDuration: Duration(seconds: 2),
        ),
        context: context,
        builder: (context) => AlertDialog(
          content: const Text('动画效果 Dialog'),
          actions: [
            TextButton(
              onPressed: () => Navigator.of(context).pop(),
              child: const Text('取消'),
            )
          ],
        ),
      );

  Widget buildFloatingActionButton() {
    return AnimatedBuilder(
      animation: controller,
      builder: (context, child) {
        return FadeScaleTransition(animation: controller, child: child);
      },
      child: Visibility(
        visible: controller.status != AnimationStatus.dismissed,
        child: FloatingActionButton(
          backgroundColor: Theme.of(context).primaryColor,
          onPressed: () {},
          child: const Icon(Icons.add),
        ),
      ),
    );
  }

  Future toggle() {
    return isForwordAnimation ? controller.reverse() : controller.forward();
  }
}
